//
//  JDMainTabBar.swift
//  JDWeiBo
//
//  Created by apple on 15/6/23.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class JDMainTabBar: UITabBar {
    
    // 记录按钮总数
    private let btn = 5
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
//        print(compoesButton)
        
        // 设置按钮大小
        let w = bounds.width / CGFloat(btn)
        
        let r = CGRectMake(0, 0, w, bounds.height)
        
        var index = 0
        
        for vs in subviews {
            
            if vs is UIControl && !(vs is UIButton) {
                
                vs.frame = CGRectOffset(r, CGFloat(index) * w, 0)
                
                index += index == 1 ? 2 : 1
            }
        }
        // 设置加号按钮的位置
        compoesButton.frame = CGRectOffset(r, 2 * w, 0)
    }
    
    // 懒加载
    lazy var compoesButton: UIButton = {
        
        let btn = UIButton()
        // 懒加载设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        // 懒加载设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        // 添加按钮
        self.addSubview(btn)
        // 返回
        return btn
    }()

}
